package com.edgclub.edg.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;

public class MyInformationChangeSignActivity extends GzBaseTooolBarActivity implements View.OnClickListener , VolleyListener{
    private boolean isload=false;
    private EditText change_sign;
    private UserPrefUtils userPrefUtils;
    private User user;

    @Override
    public int getContentView() {
        return R.layout.activity_change_sign;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("修改签名");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
        right_layout.setVisibility(View.VISIBLE);
        right_text.setText("保存");
        right_layout.setOnClickListener(this);
    }

    @Override
    public void initView() {
        change_sign = (EditText) findViewById(R.id.change_sign);
//        change_sign.addTextChangedListener(textWatcher);
    }

    @Override
    public void initData() {
        userPrefUtils = new UserPrefUtils(this);
        user=userPrefUtils.getUser();
        if(null==user)user=new User();
        if ("".equals(user.getIntro())){
            change_sign.setText("警察蜀黍，就是这个人不写签名");
            change_sign.setSelection(change_sign.length());
        }else{
            change_sign.setText(user.getIntro());
            change_sign.setSelection(change_sign.length());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_back:
                finish();
                break;
            case R.id.right_layout:
                String text=change_sign.getText().toString();
                int num= StringUtils.getChatCount(text);
                int lengh=text.length();
                if(lengh+num>28){
                    showToast("签名不能超过28字符");
                    return;
                }
                submit();
                break;
        }
    }



    private void submit() {
            upSing();
    }
    private void upSing(){
        if(isload)return;
        UserParam param = new UserParam();
        param.setUserId(user.getId());
        param.setIntro(change_sign.getText().toString()+"");
        new UserApi(this).getupsing(param,this);
    }
    @Override
    public void onStart(String tag) {
        isload=true;
        showDialog("请求中，请稍后...",false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        isload=false;
        if (JSONHelper.isSuccess(response)){
            Toast.makeText(MyInformationChangeSignActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
            if ("".equals(change_sign.getText().toString().trim())){
                user.setIntro("警察蜀黍，就是这个人不写签名");
            }else{
                user.setIntro(change_sign.getText().toString().trim());
            }
            userPrefUtils.saveUser(user);
            Intent intent=new Intent(MyInformationChangeSignActivity.this,MyInforMationActivity.class);
            intent.putExtra("user", user);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        isload=false;
        dismissDialog();
        ResultCode.toastVolleyError(MyInformationChangeSignActivity.this, volleyError);
    }
    /*private TextWatcher textWatcher = new TextWatcher() {
        private int editStart;
        private int editEnd;
        private int maxLen = 28; // the max byte
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.d("TextChanged", "---->beforeTextChanged : start = " + start + " count = " + count + " after = " + after);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            editStart = change_sign.getSelectionStart();
            editEnd = change_sign.getSelectionEnd();
            // 先去掉监听器，否则会出现栈溢出
            change_sign.removeTextChangedListener(textWatcher);
            if (!TextUtils.isEmpty(change_sign.getText())) {
                String etstring = change_sign.getText().toString().trim();
                while (calculateLength(s.toString()) > maxLen) {
                    s.delete(editStart - 1, editEnd);
                    editStart--;
                    editEnd--;
                    Log.d("TextChanged", "editStart = " + editStart + " editEnd = " + editEnd);
                }
            }

            change_sign.setText(s);
            change_sign.setSelection(editStart);

            // 恢复监听器
            change_sign.addTextChangedListener(textWatcher);
            // end by zyf --------------------------
        }
        private int calculateLength(String etstring) {
            char[] ch = etstring.toCharArray();

            int varlength = 0;
            for (int i = 0; i < ch.length; i++) {
                // changed by zyf 0825 , bug 6918，加入中文标点范围 ， TODO 标点范围有待具体化
                if ((ch[i] >= 0x2E80 && ch[i] <= 0xFE4F) || (ch[i] >= 0xA13F && ch[i] <= 0xAA40) || ch[i] >= 0x80) { // 中文字符范围0x4e00 0x9fbb
                    varlength = varlength + 2;
                } else {
                    varlength++;
                }
            }
            Log.d("TextChanged", "varlength = " + varlength);
            // 这里也可以使用getBytes,更准确嘛
            // varlength = etstring.getBytes(CharSet.forName("GBK")).lenght;// 编码根据自己的需求，注意u8中文占3个字节...
            return varlength;
        }
    };*/

}
